package com.activity.shop;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.activity.App;
import com.activity.bean.ClassBean;
import com.api.param.ClockSetParam;
import com.api.param.GroupParam;
import com.api.viewModel.StoreVM;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.example.apple.ddhr_new.R;
import com.example.apple.ddhr_new.activity.widget.Dialog;
import com.example.lzbase.BaseViewModelActivity;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

public class ClockSetActivity extends BaseViewModelActivity<StoreVM> {


    @BindView(R.id.tv_up)
    TextView tvUp;
    @BindView(R.id.l_up)
    LinearLayout lUp;
    @BindView(R.id.tv_down)
    TextView tvDown;
    @BindView(R.id.l_down)
    LinearLayout lDown;
    @BindView(R.id.tv_down1)
    TextView tvDown1;
    @BindView(R.id.tv_down2)
    TextView tvDown2;
    private boolean isup;
    private String minute;
    private ClassBean group;

    @Override
    protected int titleText() {
        return R.string.daka_shezhi;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_clock_set;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (App.getInstance().getAccount().getUserstartworkremind()==null||App.getInstance().getAccount().getUserstartworkremind().equals("0")) {

            tvUp.setText("无提醒");
        } else {
            tvUp.setText(App.getInstance().getAccount().getUserstartworkremind() + "分钟");
        }
        if (App.getInstance().getAccount().getUseroffworkremind()==null||App.getInstance().getAccount().getUseroffworkremind().equals("0")) {

            tvDown.setText("无提醒");
        } else {
            tvDown.setText(App.getInstance().getAccount().getUseroffworkremind() + "分钟");
        }


        viewModel.clockSet.observe(this, res -> {


            if (isup) {

                if (minute.equals("0")) {
                    tvUp.setText("无提醒");
                } else {
                    tvUp.setText(minute + "分钟");
                }


            } else {
                if (minute.equals("0")) {
                    tvDown.setText("无提醒");
                } else {
                    tvDown.setText(minute + "分钟");
                }

            }
            res.setPwd(App.getInstance().getAccount().getPwd());
            App.getInstance().setAccount(res);

        });

        viewModel.clockSetRange.observe(this, res -> {


            if (isup) {

                if (minute.equals("0")) {
                    tvDown1.setText("0分钟");
                } else {
                    tvDown1.setText(minute + "分钟");
                }
                group.setAttendancegroupstartworktimerange(minute);


            } else {
                if (minute.equals("0")) {
                    tvDown2.setText("0分钟");
                } else {
                    tvDown2.setText(minute + "分钟");
                }
                group.setAttendancegroupoffworktimerange(minute);
            }


        });

        viewModel.attanceGroupInfo.observe(this,res->{


            group =res.get(0);
            if (group.getAttendancegroupstartworktimerange()==null||group.getAttendancegroupstartworktimerange().equals("0")) {

                tvDown1.setText("0分钟");
            } else {
                tvDown1.setText(group.getAttendancegroupstartworktimerange() + "分钟");
            }
            if (group.getAttendancegroupoffworktimerange()==null||group.getAttendancegroupoffworktimerange().equals("0")) {

                tvDown2.setText("0分钟");
            } else {
                tvDown2.setText(group.getAttendancegroupoffworktimerange() + "分钟");
            }

        });
        GroupParam param =new GroupParam();
        param.attendancegroupid = App.getInstance().getAccount().getAttendancegroupinfo().get(0).getAttendancegroupid();

        viewModel.getGroupInfo(param);


    }

    @OnClick({R.id.l_up, R.id.l_down,R.id.l_down1,R.id.l_down2})
    public void onClick(View view) {

        ArrayList<String> arrayList = new ArrayList<>();

        arrayList.add("无提醒");

        arrayList.add("5分钟");
        arrayList.add("10分钟");
        arrayList.add("15分钟");
        arrayList.add("30分钟");
        switch (view.getId()) {
            case R.id.l_up: {


                OptionsPickerView optionsPickerView = Dialog.popWheel(ClockSetActivity.this, new OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int options2, int options3, View v) {


                        String res = arrayList.get(options1);
                        ClockSetParam param = new ClockSetParam();
                        if (options1 == 0) {
                            param.startworkremind = "0";
                        } else {
                            param.startworkremind = res.replace("分钟", "");
                        }

                        isup = true;

                        minute = param.startworkremind;

                        param.offworkremind = App.getInstance().getAccount().getUseroffworkremind();


                        addSubscription(viewModel.setClock(param));


                    }
                });
                int index = 0;


                if (arrayList.contains(tvUp.getText().toString())) {
                    index = arrayList.indexOf(tvUp.getText().toString());
                } else {

                }


                optionsPickerView.setSelectOptions(index);
                optionsPickerView.setPicker(arrayList);
                optionsPickerView.show();
            }
            break;
            case R.id.l_down: {


                OptionsPickerView optionsPickerView = Dialog.popWheel(ClockSetActivity.this, new OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int options2, int options3, View v) {


                        String res = arrayList.get(options1);

                        ClockSetParam param = new ClockSetParam();
                        if (options1 == 0) {
                            param.offworkremind = "0";
                        } else {
                            param.offworkremind = res.replace("分钟", "");
                        }
                        isup = false;

                        minute = param.offworkremind;

                        param.startworkremind = App.getInstance().getAccount().getUserstartworkremind();


                        addSubscription(viewModel.setClock(param));


                    }
                });
                int index = 0;


                if (arrayList.contains(tvDown.getText().toString())) {
                    index = arrayList.indexOf(tvDown.getText().toString());
                } else {

                }


                optionsPickerView.setSelectOptions(index);
                optionsPickerView.setPicker(arrayList);
                optionsPickerView.show();

            }
            break;
            case R.id.l_down1:


            {
                ArrayList<String> arrayList1 = new ArrayList<>();

                arrayList1.add("0分钟");

                arrayList1.add("5分钟");
                arrayList1.add("10分钟");
                arrayList1.add("15分钟");
                arrayList1.add("30分钟");
                OptionsPickerView optionsPickerView = Dialog.popWheel(ClockSetActivity.this, new OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int options2, int options3, View v) {


                        String res = arrayList.get(options1);
                        ClockSetParam param = new ClockSetParam();
                        if (options1 == 0) {
                            param.startworktimerange  = "0";
                        } else {
                            param.startworktimerange  = res.replace("分钟", "");
                        }

                        isup = true;

                        minute = param.startworktimerange;

                        param.offworktimerange = ((null==group||group.getAttendancegroupoffworktimerange()==null)?"0":group.getAttendancegroupoffworktimerange());

                        param.attendancegroupno=App.getInstance().getAccount().getAttendancegroupinfo().get(0).getAttendancegroupno();
                        addSubscription(viewModel.setClockRange(param));


                    }
                });
                int index = 0;


                if (arrayList1.contains(tvDown1.getText().toString())) {
                    index = arrayList.indexOf(tvDown1.getText().toString());
                } else {

                }


                optionsPickerView.setSelectOptions(index);
                optionsPickerView.setPicker(arrayList1);
                optionsPickerView.show();
            }
                break;
            case R.id.l_down2:
            {
                ArrayList<String> arrayList1 = new ArrayList<>();

                arrayList1.add("0分钟");

                arrayList1.add("5分钟");
                arrayList1.add("10分钟");
                arrayList1.add("15分钟");
                arrayList1.add("30分钟");
                OptionsPickerView optionsPickerView = Dialog.popWheel(ClockSetActivity.this, new OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int options2, int options3, View v) {


                        String res = arrayList.get(options1);
                        ClockSetParam param = new ClockSetParam();
                        if (options1 == 0) {
                            param.offworktimerange  = "0";
                        } else {
                            param.offworktimerange  = res.replace("分钟", "");
                        }

                        isup = false;

                        minute = param.offworktimerange;

                        param.startworktimerange = (null==group||group.getAttendancegroupstartworktimerange()==null)?"0":group.getAttendancegroupstartworktimerange();
                        param.attendancegroupno=App.getInstance().getAccount().getAttendancegroupinfo().get(0).getAttendancegroupno();

                        addSubscription(viewModel.setClockRange(param));


                    }
                });
                int index = 0;


                if (arrayList1.contains(tvDown1.getText().toString())) {
                    index = arrayList.indexOf(tvDown1.getText().toString());
                } else {

                }


                optionsPickerView.setSelectOptions(index);
                optionsPickerView.setPicker(arrayList1);
                optionsPickerView.show();
            }
                break;

                default:
                    break;

        }
    }


}
